<script>

import { GET_LOGIN_ID, PAGE_INFO } from '@/utils/MyUtils'
import CategoryEdit from '@/views/admin/category/CategoryEdit.vue'

export default {
  name: 'CategoryList',
  components: { CategoryEdit },
  data () {
    return {
      type: '',
      searchInfo: {
        createUserId: '',
        name: '',
        status: ''
      },
      pageInfo: {
        ...PAGE_INFO
      },
      tableList: [],
      delIds: []
    }
  },
  methods: {
    init () {
      this.pageInfo = { ...this.$options.data().pageInfo }
      this.searchInfo = { ...this.$options.data().searchInfo }
      this.type = this.$route.meta.type
      if (this.type === 'vip') {
        this.searchInfo.createUserId = GET_LOGIN_ID()
      } else {
        this.searchInfo.createUserId = ''
      }
      this.getTableList()
    },
    getTableList () {
      this.$axios.get('category/page', { params: { ...this.pageInfo, ...this.searchInfo } }).then(data => {
        this.tableList = [...data.records]
        this.pageInfo.count = data.total
      })
    },
    currentChange (value) {
      this.pageInfo.page = value
      this.getTableList()
    },
    sizeChange (value) {
      this.pageInfo.page = 1
      this.pageInfo.limit = value
      this.getTableList()
    },
    selectionChange (selection) {
      this.delIds = [...selection.map(item => item.categoryId)]
    },
    search () {
      this.pageInfo.page = 1
      this.getTableList()
    },
    clear () {
      this.pageInfo = { ...this.$options.data().pageInfo }
      this.searchInfo = { ...this.$options.data().searchInfo }
      this.getTableList()
    },
    deleteData (id) {
      if (!id && this.delIds.length === 0) {
        this.$alert('请至少选中1条进行删除！', { type: 'warning' })
        return false
      }
      const deleteIds = id ? [id] : [...this.delIds]
      this.$confirm(`确定删除[${deleteIds}]的数据吗?`, { type: 'info', title: '询问' }).then(() => {
        this.$axios.delete('category/delete', { data: deleteIds }).then(() => {
          this.$message.success('操作成功！')
          this.getTableList()
        }).catch(e => e)
      })
    },
    editData (id) {
      this.$refs.editRef.init(id)
    },
    statusChange (id) {
      this.$confirm(`确定更改编号【${id}】的状态吗?`, { type: 'info', title: '询问' }).then(() => {
        this.$axios.post('category/status/' + id).then(() => {
          this.$message.success('操作成功！')
          this.getTableList()
        }).catch(e => e)
      }).catch(e => e)
    }
  },
  watch: {
    '$route.path': function () {
      this.init()
    }
  },
  created () {
    this.$nextTick(() => {
      this.init()
    })
  }
}
</script>

<template>
  <div>
    <el-card>
      <el-form :model="searchInfo" inline label-width="80px">
        <el-form-item label="分类名">
          <el-input v-model="searchInfo.name" placeholder="分类名" clearable/>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchInfo.status" placeholder="选择状态" clearable>
            <el-option :value="1" label="可用" />
            <el-option :value="2" label="禁用" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="search">查询</el-button>
          <el-button @click="clear">清空</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button v-if="searchInfo.createUserId" @click="editData()">添加</el-button>
      <el-button @click="deleteData()">删除</el-button>
      <el-table @selection-change="selectionChange" :data="tableList" border>
        <el-table-column type="selection" fixed="left"/>
        <el-table-column prop="categoryId" label="编号"/>
        <el-table-column prop="name" label="分类名"/>
        <el-table-column prop="profile" label="简介" show-overflow-tooltip/>
        <el-table-column v-if="!searchInfo.createUserId" prop="createUserinfo.username" label="创建人"/>
        <el-table-column label="状态">
          <template slot-scope="{row}">
            <el-button @click="statusChange(row.categoryId)" title="点击切换状态" type="success" v-if="row.status === 1">可用</el-button>
            <el-button @click="statusChange(row.categoryId)" title="点击切换状态" type="danger" v-else>禁用</el-button>
          </template>
        </el-table-column>
        <el-table-column fixed="right" width="180px" label="操作">
          <template slot-scope="{row}">
            <el-button v-if="searchInfo.createUserId" @click="editData(row.categoryId)">修改</el-button>
            <el-button @click="deleteData(row.categoryId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="pageInfo.page" :page-size="pageInfo.limit" :page-sizes="[5,10,20,50,100]" :total="pageInfo.count" layout="total,prev,pager,next,jumper,sizes" @current-change="currentChange" @size-change="sizeChange"/>
    </el-card>
    <category-edit ref="editRef" @close="init"/>
  </div>
</template>

<style scoped lang="scss">

</style>
